package com.common.www;

import com.common.www.data.Constant;
import com.common.www.service.file.FileService;
import com.common.www.service.global.GlobalConfigService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

@Component
public class MyApplicationListener implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    GlobalConfigService configService;
    @Autowired
    Constant constant;
    @Autowired
    FileService fileService;

    public static String APPLICATION_NAME = "";

    /**
     * 是否是发布模式(非发布模式下会有一些调试代码)
     */
    public static boolean isRelease = true;
    public static String projectPath = "";//项目外部访问地址
    public static String publicProjectPath = "";//项目外部访问地址
    public static String fileRootPath = "";//项目文件本地根地址 需要后面跟 斜杠
    static MyApplicationListener instance = null;

    static public MyApplicationListener getInstance() {
        if (instance == null) {
            instance = new MyApplicationListener();
        }
        return instance;
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("项目启动成功");
        APPLICATION_NAME = constant.getAPPLICATION_NAME();
        initReleaseData(configService);

    }


    public static String QNaccessKey = "";
    public static String QNsecretKey = "";
    public static String QNbucket = "";
    public static String QNdomainOfBucket = "";

    /**
     * 初始化调试状态切换的一个方法
     */
    public void initReleaseData(GlobalConfigService configService) {
        isRelease = configService.isRelease();
        projectPath = fileService.getProjectPath();
        publicProjectPath = fileService.getPublicProjectPath();
        fileRootPath = fileService.getFileRootPath();
        if (isRelease) {//发布的七牛key
            QNaccessKey = "n__GpJvx52zQ_glmsgMWbI6FeDGyMzcdxvqAVpp8";
            QNsecretKey = "f9pQLE1A5bmCPoDjcka_JQlQQ_Z7asUg63wrwmOp";
            QNbucket = "designprod";
            QNdomainOfBucket = "http://ydtprod.xsdr.top";
        } else {
            QNaccessKey = "n__GpJvx52zQ_glmsgMWbI6FeDGyMzcdxvqAVpp8";
            QNsecretKey = "f9pQLE1A5bmCPoDjcka_JQlQQ_Z7asUg63wrwmOp";
            QNbucket = "testdesign";
            QNdomainOfBucket = "http://testdesign2.luozisong.top";
        }
    }
}
